<?php
class IndexController extends My_Controller_Action{
    public $wpdb=null;
    public function init()
    {
       
        $this->loadTemplates('admin');
        $this->view->headScript()->prependFile('../public/js/languageswitcher.js');
        $this->view->headLink()->appendStylesheet("../public/js/languageswitcher.css");
        $this->wpdb=new Default_Model_Book();
    }
    public function indexAction()
    {
   
        $this->view->books=$this->wpdb->listAll();
    }
    public function addAction()
    {
         $form=new Default_Form_add;
        $mauthor=new Default_Model_Author;
        $author_value=$mauthor->listAuthor();
        if(!empty($author_value))
        {
            $temp=array();
            foreach($author_value as $key => $value )
            {
                $temp[$value["au_id"]]=$value["au_name"];
            }
           $form->bk_author->setMultiOptions($temp);
        }
       // $author=$this->wpdb->listAuthor();
        $this->view->headScript()->prependFile("../public/js/customjs/add.js");
        $this->view->headScript()->prependFile("../public/js/ckeditor/ckeditor.js");
      
        if($this->_request->isPost())
        {
            $data=$this->_request->getPost();
      
            if($form->isValid($data))
            {
                    
               // print_r($data);
               // die();
             //  var_dump(is_dir(getcwd()."/public/data/")); die();
                $upload=new Zend_File_Transfer_Adapter_Http;
                $upload->setDestination(getcwd()."/public/data/");
                try{
                    $upload->receive();
                    if($upload->isUploaded())
                    {
                        $insertData=array();
                        $insertData["bk_image"]=$upload->getFileName("bk_image",false);
          
                    }
                    else
                    {
                        print("error");
                        die();
                    }
                } catch (Zend_File_Transfer_Exception $e) {
                     $e->getMessage();
                }
              
                $insertData["bk_name"]=$this->_request->getPost('bk_name');
                $insertData["bk_author"]=$this->_request->getPost('bk_author');
                $insertData["bk_price"]=$this->_request->getPost('bk_price');
                $insertData["bk_description"]=$this->_request->getPost('bk_description');
             
                $id = $this->wpdb->insert($insertData);
                if($id != null )
                {
                    $this->_helper->redirector->gotoUrl('index/detail/id/'.$id);
                    exit();
                } 
            }
            else
            {
                $form->populate($data);
            }
        }
        $form->setAttrib('enctype', 'multipart/form-data');
        $form->submit->setLabel('Add Book');
        $form->setAction('add');
        $this->view->addFrm=$form;
    }
    public function editAction()
    {
        $mauthor=new Default_Model_Author;
         $form=new Default_Form_add;
        $author_value=$mauthor->listAuthor();
        if(!empty($author_value))
        {
            $temp=array();
            foreach($author_value as $key => $value )
            {
                $temp[$value["au_id"]]=$value["au_name"];
            }
           $form->bk_author->setMultiOptions($temp);
        }
        $this->view->headScript()->appendFile($this->view->baseUrl()."/public/js/customjs/add.js");
        $this->view->headScript()->appendFile($this->view->baseUrl()."/public/js/ckeditor/ckeditor.js");
        
        $id=$this->getRequest()->getParam('id');
        $book=$this->wpdb->listAll(false,$id);
          if($this->_request->isPost())
         {
            $data=$this->_request->getPost();
           
            if($form->isValid($data))
            {
                // do some thing
                $update_data=array();
                $upload=new Zend_File_Transfer_Adapter_Http;
                $upload->setDestination(getcwd()."/public/data/");
                if($upload->isUploaded() && $_FILES["bk_image"]["error"]==0 )
                {
                    unlink($this->view->baseUrl("public/data/".$book["bk_image"]));
                    $update_data["bk_image"]=$upload->getFileName("bk_image",false);
                }
                $update_data["bk_name"]=$data["bk_name"];
                $update_data["bk_price"]=$data["bk_price"];
                $update_data["bk_author"]=$data["bk_author"];
                $update_data["bk_description"]=$data["bk_description"];
                
                $update_effect=$this->wpdb->update($update_data,"bk_id = ".$id );
            //    Zend_Debug::dump($this->wpdb->up);
           // Zend_Debug::dump($update_data);
                if($update_effect > 0)
                {
                    $this->_helper->redirector->gotoUrl('index/detail/id/'.$id);
                    exit();
                }
                else
                {
                    echo $update_effect; die();
                }
                // $form->populate($data);
            }
            else
            {
                $form->populate($data);
            }
        }
         $form->bk_name->setValue($book["bk_name"]);
         $form->bk_price->setValue($book["bk_price"]);
         $form->bk_description->setValue($book['bk_description']);
         $form->bk_image->setValue($book['bk_image']);
        
        $form->setAction($this->view->baseUrl("index/edit/id/".$book["bk_id"]));
        $form->bk_author->setValue($book['bk_author']);
        $form->set_image($this->view->baseUrl("public/data/".$book["bk_image"]) );
      
        $this->view->editFrm=$form;

    }
    public function deleteAction()
    {
        $id=$this->_request->getParam("id");
        if($id != null && is_numeric($id))
        {
            try{
                $this->wpdb->delete("bk_id =".$id);
                
            }catch(Zend_Exception $e )
            {
                echo $e->message;
            }
        }
         $this->_helper->redirector->gotoUrlAndExit("index");
    }
    public function detailAction()
    {
      
        $id= $this->getRequest()->getParam('id');
        $book=$this->wpdb->listAll(false,$id);
        
        $this->view->book=$book;
    }
    
}